package StatuSync::Web::Admin;
use strict;

# admin web interface

use Template;

use base qw(StatuSync::Log);
use StatuSync::User;

sub init {
	my $self = shift;
	return $self;
}

sub handler {
	my $self = shift;
	my ($httpd, $req) = @_;
	$self->debug($req->url);

	my $out;
	if ($req->method eq 'POST') {
        if ($req->url =~ m{^/admin/user/(\d+)/$}) {
			my $user = StatuSync::User->find({ id => $1 });
			if ($req->parm('delete')) {
				$user->delete;
			}
			else {
				$user->update({ $req->vars });
			}
		}
		elsif ($req->url =~ m{^/admin/user/$}) {
			StatuSync::User->create({ $req->vars });
		}
		$req->respond({ redirect => '/admin/' });
		$httpd->stop_request;
		kill HUP => $$;
		return;
	}
	elsif ($req->method eq 'GET') {
		if ($req->url =~ m{^/admin/user/(?:(\d+)/)?$}) {
			$out = $self->user($1);
		}
		elsif ($req->url =~ m{^/admin/$}) {
			$out = $self->list();
		}
	}

	my $headers = { 'Content-Type' => 'text/html' };

	$req->respond( defined $out ? [ 200, 'OK', $headers, $out ] :
		[ 404, 'Not Found"', $headers, "404 Not Found" ]);
	$httpd->stop_request;
}

my $tt;
sub tt {
	my $self = shift;
	my ($template, $data) = @_;
	unless ($tt) {
		$tt = Template->new({
			BLOCKS => {
				wrapper => <<'END',
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>[% title || "Statusync" %]</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
</head>
<body>
[% content %]
</body>
</html>
END
			},
			WRAPPER => 'wrapper',
		}) or die $Template::ERROR;
	}
	my $out;
	$tt->process($template, $data, \$out) or die $Template::ERROR;
	return $out;
}

sub list {
	my $self = shift;
	my @users = StatuSync::User->search();
	return $self->tt(\q{
<h1>Users list</h1>
<a href="/admin/user/">Add user</a>
<ul>
[% FOREACH user = users %]
<li>
[% user.id %]
[% user.email %]
<a href="http://twitter.com/[% user.tw_username %]">@[% user.tw_username %]</a>
<a href="/admin/user/[% user.id %]/">edit</a>
</li>
[% END %]
</ul>
		}, {
			users => \@users,
		});
}

sub user {
	my $self = shift;
	my $id = shift;
	my $user = StatuSync::User->find({ id => $id }) if $id;
	return $self->tt(\<<'END',
<h1>[% user ? "User $user.id" : "New user" | html %]</h1>
<form method="POST">

<label>Id</label>
<span>[% user.id | html %]</span>

<label for="f-email">Email</label>
<input type="text" name="email" id="f-email" value="[% user.email | html %]" />

<label for="f-password">Password</label>
<input type="text" name="password" id="f-password" value="[% user.password | html %]" />

<label for="f-tw_username">Twitter username</label>
<input type="text" name="tw_username" id="f-tw_username" value="[% user.tw_username | html %]" />

<label for="f-tw_id">Twitter id</label>
<input type="text" readonly="readonly" name="tw_id" id="f-tw_id" value="[% user.tw_id | html %]" />

<script type="text/javascript">
	$('#f-tw_username').change(function(){
		$.getJSON("http://twitter.com/users/show.json?callback=?", {
			screen_name: $(this).val(),
		}, function(data){
			$('#f-tw_id').val(data.id);
		});
	});
</script>

<label for="f-vk_email">VKontakte.ru email</label>
<input type="text" name="vk_email" id="f-vk_email" value="[% user.vk_email | html %]" />

<label for="f-vk_password">VKontakte.ru password</label>
<input type="text" name="vk_password" id="f-vk_password" value="[% user.vk_password | html %]" />

<input type="checkbox" name="delete" id="cb-delete" value="1" />
<label for="cb-delete">Delete this user</label>

<input type="submit" id="btn-submit" value="Save" />
</form>
END
		{
			user => $user,
		});
}

1;

